﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace giaodien
{
    public partial class frmLoai : Form
    {
        public frmLoai()
        {
            InitializeComponent();
        }

        DataSet dsl = new DataSet();
        ConnectData cd = new ConnectData();
        int f;
        static Thread ThreadQLNhapHang;

        #region chucnang
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            UI.MouseDown(e);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            UI.MouseMove(this, MousePosition.X, MousePosition.Y);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            UI.MouseUp();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            ThreadQLNhapHang.Start();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            
        }

        private void btnMini_Click(object sender, EventArgs e)
        {
            UI.Mini(this);
        }

        static void MoLaiQLNhapHang()
        {
            Application.Run(new QLNhapHang());
        }
        #endregion

        #region ham
        void hienthilistview(DataTable dt)
        {
            lvwloaisp.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem it = new ListViewItem();
                it.Text = dr["maloai"].ToString();
                it.SubItems.Add(dr["tenloai"].ToString());
                if ((Boolean)dr["ttkichco"] == true)
                    it.SubItems.Add("C");
                else
                    it.SubItems.Add("S");
                lvwloaisp.Items.Add(it);
            }
        }

        void lammoi()
        {
            txtmaloai.Text = "";
            txttenloai.Text = "";
            chktheochu.Checked = false;
            chktheoso.Checked = false;
        }

        int kiemtrathongtintrung()
        {
            int flag = 0;
            foreach (ListViewItem lvw in lvwloaisp.Items)
            {
                if ((txtmaloai.Text == lvw.SubItems[0].Text) || (txttenloai.Text == lvw.SubItems[1].Text))
                {
                    flag = 1;
                    if (txttenloai.Text == lvw.SubItems[1].Text)
                        flag = 2;
                }
            }
            return flag;
        }

        void trangthaibtn(Boolean b)
        {
            btnthem.Enabled = b;
            btnsua.Enabled = b;
            btnxoa.Enabled = b;
        }

        void trangthaitxt(Boolean t)
        {
            txtmaloai.ReadOnly = t;
            txttenloai.ReadOnly = t;
        }

        void trangthaichk(Boolean c)
        {
            chktheochu.Enabled = c;
            chktheoso.Enabled = c;
        }
        #endregion

        private void frmLoai_Load(object sender, EventArgs e)
        {
            ThreadQLNhapHang = new Thread(new ThreadStart(MoLaiQLNhapHang));
            trangthaibtn(false);
            btnthem.Enabled = true;
            trangthaitxt(true);
            trangthaichk(false);
            dsl = cd.laydulieuDTS("select * from loai");
            hienthilistview(dsl.Tables[0]);
        }

        private void lvwloaisp_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                trangthaitxt(true);
                trangthaibtn(true);
                trangthaichk(false);
                ListViewItem it = new ListViewItem();
                if (it.SubItems.ToString() == "")
                    it.SubItems.Add("");
                else
                {
                    it = lvwloaisp.GetItemAt(e.X, e.Y);
                    txtmaloai.Text = it.Text;
                    txttenloai.Text = it.SubItems[1].Text;
                    if (it.SubItems[2].Text == "C")
                        chktheochu.Checked = true;
                    else
                        chktheoso.Checked = true;
                }
            }
            catch
            {
                MessageBox.Show("");
            }
        }

        private void chktheochu_CheckedChanged(object sender, EventArgs e)
        {
            if (chktheochu.Checked)
                chktheoso.Checked = false; 
        }

        private void chktheoso_CheckedChanged(object sender, EventArgs e)
        {
            if (chktheoso.Checked)
                chktheochu.Checked = false; 
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            f = 1;
            lammoi();
            txtmaloai.Focus();
            trangthaitxt(false);
            trangthaichk(true);
            trangthaibtn(false);
            lvwloaisp.Enabled = false;
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            f = 2;
            txttenloai.Focus();
            trangthaitxt(false);
            txtmaloai.ReadOnly = true;
            trangthaibtn(false);
            trangthaichk(true);
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DialogResult d = MessageBox.Show("Bạn có muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                if (cd.xoaloai(txtmaloai.Text) > 0)
                {
                    MessageBox.Show("Xóa thành công");
                    dsl = cd.laydulieuDTS("select * from loai");
                    trangthaibtn(false);
                    btnthem.Enabled = true;
                    lammoi();
                    hienthilistview(dsl.Tables[0]);
                }
                else
                    MessageBox.Show("Xóa không thành công");
            }
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            if (f == 1)
            {
                if (txtmaloai.Text == "" || txttenloai.Text == "" || chktheochu.Checked == false && chktheoso.Checked == false)
                { MessageBox.Show("Mời bạn nhập thông tin"); }
                else
                {
                    if (kiemtrathongtintrung() != 0)
                    { MessageBox.Show("Thông tin bạn nhập bị trùng."); }
                    else
                    {
                        if (cd.themloai(txtmaloai.Text, txttenloai.Text, chktheochu.Checked) > 0)
                        {
                            MessageBox.Show("Thêm thành công");
                            dsl = cd.laydulieuDTS("select * from loai");
                            trangthaitxt(true);
                            lvwloaisp.Enabled = true;
                            btnthem.Enabled = true;
                            trangthaichk(false);
                            lammoi();
                            hienthilistview(dsl.Tables[0]);
                        }
                        else
                            MessageBox.Show("Thêm không thành công");
                    }
                }
            }
            else
            {
                try
                {
                    if (txtmaloai.Text == "" || txttenloai.Text == "" || chktheochu.Checked == false && chktheoso.Checked == false)
                    { MessageBox.Show("Mời bạn nhập thông tin"); }
                    else
                    {
                        //if (kiemtrathongtintrung() == 2)
                        //{ MessageBox.Show("Thông tin bạn nhập bị trùng."); }
                        //else
                        //{
                        if (cd.sualoai(txtmaloai.Text, txttenloai.Text, chktheochu.Checked) > 0)
                        {
                            MessageBox.Show("Sửa thành công");
                            dsl = cd.laydulieuDTS("select * from loai");
                            trangthaitxt(true);
                            lvwloaisp.Enabled = true;
                            btnthem.Enabled = true;
                            trangthaichk(false);
                            hienthilistview(dsl.Tables[0]);
                            lammoi();
                        }
                        else
                            MessageBox.Show("Sửa không thành công");
                    }
                }
                catch
                {
                    MessageBox.Show("Thông tin bạn nhập bị trùng.");
                }
            }
        }

        private void btnquaylai_Click(object sender, EventArgs e)
        {
            frmLoai_Load(sender, e);
            lammoi();
            lvwloaisp.Enabled = true;
        }
    }
}
